package cn.zc.curriculum.service.controller.admin;


import cn.tools.core.VO.PageVO;
import cn.tools.core.base.R;
import cn.zc.curriculum.entity.Dto.CurriculumImgDto;
import cn.zc.curriculum.service.service.CurriculumImgService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Api(tags = "后台图片上传管理接口")
@RestController
@RequestMapping("/curriculum")
public class AdminCurriculumImgController {

    @Resource
    private CurriculumImgService curriculumImgService;

    /**
     * 上传图片
     * @param image
     * @param imgType
     * @return
     */
    @ApiOperation(value = "上传图片")
    @PostMapping("/admin/create/CurriculumImg")
    private R<Object> createCurriculumImg(MultipartFile image,String imgType){
        String id = curriculumImgService.saveImgToData(image,imgType);
        return R.success(id);
    }

    /**
     * 删除图片，注意，删除图片会同时删除在oss与本地的文件
     * @param id
     * @return
     */
    @ApiOperation(value = "删除图片，并删除oss与本地文件")
    @DeleteMapping("/admin/delete/CurriculumImg")
    private R deleteCurriculumImg(String id){
        curriculumImgService.deleteCurriculumImg(id);
        return R.success();
    }


    /**
     * 根据id或者type查询出数据库中保存的img数据信息
     * @param pageVO
     * @param id
     * @param imgType
     * @return
     */
    @ApiOperation(value = "根据id或者type查询出图片")
    @GetMapping("/admin/find/CurriculumImg")
    private R<Object> getCurriculumImg(PageVO pageVO, String id, String imgType){
        List<CurriculumImgDto> curriculumImgDto = curriculumImgService.getCurriculumImg(pageVO,id,imgType);
        return R.success(curriculumImgDto);
    }

    /**
     * 根据id获取本地图片文件数据以base64形式返回，用于获取本地图片使用
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id获取出本地的数据文件，以base64返回数据")
    @GetMapping("/admin/find/LocalFileData")
    private R<Object> findLocalFileData(String id, HttpServletResponse response){
        String imgDate = curriculumImgService.findLocalFileData(id,response);
        return R.success(imgDate);
    }


    /**
     * 通过id流形式获取本地存储图片
     * @param request
     * @param response
     * @param imgId
     */
    @ApiOperation(value = "通过id流形式获取图片，方便前端测试，之所以不给你路径访问，是防止出现安全漏洞，注意这个只读本地存储的文件")
    @GetMapping("/admin/find/ImageStream")
    private void findImageStream(HttpServletRequest request, HttpServletResponse response,String imgId) {
        curriculumImgService.findImageStream(request,response,imgId);
    }


}
